#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#endregion

namespace Secure.Client
{
    public class XmlSecureClient : ISecureClient
    {
        private readonly Dictionary<string, List<UserRole>> _roles = 
            new Dictionary<string, List<UserRole>>();

        public bool IsUserInRole(string username, string roleName, out string[] additionalParams)
        {
            if (_roles.ContainsKey(roleName))
            {
                UserRole role = _roles[roleName].Find(x => x.Username == username);
                if (role != null)
                {
                    additionalParams = role.AdditionalParams;
                    return true;
                }
            }

            additionalParams = new string[0];
            return false;
        }

        public void AddUserToRole(string username, string roleName, params string[] additionalParams)
        {
            List<UserRole> users;
            if (!_roles.TryGetValue(roleName, out users))
            {
                users = new List<UserRole>();
                _roles.Add(roleName, users);
            }

            users.Add(new UserRole(username, additionalParams));
        }

        public void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new System.NotImplementedException();
        }

        public void CreateRole(string roleName)
        {
            if (!_roles.ContainsKey(roleName))
            {
                _roles.Add(roleName, new List<UserRole>());
            }
        }

        private class UserRole
        {
            private readonly string _username;
            private readonly string[] _additionalParams;

            public UserRole(string username, string[] additionalParams)
            {
                _username = username;
                _additionalParams = additionalParams;
            }

            public string Username
            {
                get { return _username; }
            }

            public string[] AdditionalParams
            {
                get { return _additionalParams; }
            }
        }
    }
}